package main.java.indi.zyj.coderecord;

import main.java.indi.zyj.hot100.base.TreeNode;

public class DeleteNode {
    public TreeNode deleteNode(TreeNode root, int key) {

        if (root == null) {
            return null;
        }

        if (root.val == key) {
            if (root.left == null) {
                return root.right;
            } else if (root.right == null) {
                return root.left;
            } else {
                TreeNode curNode = root.right;
                while (curNode.left != null) {
                    curNode = curNode.left;
                }
                curNode.left = root.left;
                root = root.right;
                return root;
            }
        }
        if (root.val > key) root.left = deleteNode(root.left, key);
        if (root.val < key) root.right = deleteNode(root.right, key);
        return root;


    }
}
